﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZDSoft.LMS.Domain;
using NHibernate.Criterion;
using NHibernate;
using Castle.ActiveRecord;


namespace ZDSoft.LMS.Manager
{
    public class DeliveryFormManager:ManagerBase<DeliveryForm>
    {
        public IList<string> FindDistinctYear()
        {
            IList<string> years = new List<string>();
            IList<TransportOrder> orders = new TransportOrderManager().Find(new List<ICriterion>() { Expression.Eq("Status", (int)TransportOrderStatus.Done) });
            foreach (TransportOrder to in orders)
            {
                if (years.Where(o => o.ToString() == to.CreateDate.ToShortDateString()).Count() > 0)
                {
                    continue;
                }
                years.Add(to.CreateDate.Year.ToString());
            }
            return years;
        }

        public IList<DeliveryForm> DemoSimpleQuery(int pageIndex, int pageSize, string transporter, string deliveryDate, string commisionder, string receiver)
        {
            #region 构造HQL查询语句
            StringBuilder hql = new StringBuilder(@"from " + typeof(DeliveryForm).Name + " d");
            if (!string.IsNullOrEmpty(transporter))
            {
                AddHqlSatements(hql);
                hql.Append("d.Transporter.UserName = :transporter");
            }
            if (!string.IsNullOrEmpty(deliveryDate))
            {
                AddHqlSatements(hql);
                hql.Append("d.DeliveryDate = :deliveryDate");
            }
            if (!string.IsNullOrEmpty(commisionder))
            {
                AddHqlSatements(hql);
                hql.Append("d.TransportOrder.Commisionedor.Name = :commisionder");
            }
            if (!string.IsNullOrEmpty(receiver))
            {
                AddHqlSatements(hql);
                hql.Append("d.TransportOrder.Receiver.Name = :receiver");
            }
            #endregion

            ISession session = ActiveRecordBase.holder.CreateSession(typeof(DeliveryForm));
            IQuery query = session.CreateQuery(hql.ToString());
            IQuery queryScalar = session.CreateQuery("select count(ID) " + hql.ToString());
            if (!string.IsNullOrEmpty(transporter))
            {
                queryScalar.SetParameter("transporter", transporter);
                query.SetString("transporter", transporter);
            }
            if (!string.IsNullOrEmpty(deliveryDate))
            {
                queryScalar.SetParameter("deliveryDate", deliveryDate);
                query.SetString("deliveryDate", deliveryDate);
            }
            if (!string.IsNullOrEmpty(commisionder))
            {
                queryScalar.SetParameter("commisionder", commisionder);
                query.SetString("commisionder", commisionder);
            }
            if (!string.IsNullOrEmpty(receiver))
            {
                queryScalar.SetParameter("receiver", receiver);
                query.SetString("receiver", receiver);
            }

            object count = queryScalar.List();
            query.SetFirstResult((pageIndex - 1) * pageSize);
            query.SetMaxResults(pageSize);
            IList<DeliveryForm> arr = query.List<DeliveryForm>();
            session.Close();
            return arr;
        }
    }
}
